package com.smartscreen.contentmanage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.smartscreen.contentmanage.entity.DisplayTemplate;
import com.smartscreen.contentmanage.service.DisplayTemplateService;
import com.smartscreen.common.response.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/content/classes")
@Validated
public class DisplayTemplateController {
    
    @Autowired
    private DisplayTemplateService displayTemplateService;
    
    /**
     * 获取班级显示模板
     * @param classId 班级ID
     * @return 模板信息
     */
    @GetMapping("/{classId}/display-template")
    public ApiResponse<Map<String, Object>> getTemplate(@PathVariable String classId) {
        try {
            if (classId == null || classId.trim().isEmpty()) {
                return ApiResponse.badRequest("班级ID不能为空");
            }
            
            QueryWrapper<DisplayTemplate> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("class_id", classId);
            
            DisplayTemplate template = displayTemplateService.getOne(queryWrapper);
            
            if (template != null) {
                Map<String, Object> data = new HashMap<>();
                data.put("templateId", template.getTemplateId());
                data.put("layout", template.getLayout());
                return ApiResponse.success("获取模板成功", data);
            } else {
                return ApiResponse.notFound("模板不存在");
            }
        } catch (Exception e) {
            return ApiResponse.serverError("获取模板失败：" + e.getMessage());
        }
    }
    
    /**
     * 更新班级显示模板
     * @param classId 班级ID
     * @param request 模板配置
     * @return 更新结果
     */
    @PutMapping("/{classId}/display-template")
    public ApiResponse<Void> updateTemplate(
            @PathVariable String classId,
            @RequestBody Map<String, Object> request) {
        
        try {
            if (classId == null || classId.trim().isEmpty()) {
                return ApiResponse.badRequest("班级ID不能为空");
            }
            
            if (request == null || !request.containsKey("layout")) {
                return ApiResponse.badRequest("模板布局配置不能为空");
            }
            
            QueryWrapper<DisplayTemplate> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("class_id", classId);
            
            DisplayTemplate template = displayTemplateService.getOne(queryWrapper);
            if (template == null) {
                template = new DisplayTemplate();
                template.setClassId(classId);
            }
            
            template.setLayout((Map<String, Object>) request.get("layout"));
            
            boolean success = displayTemplateService.saveOrUpdate(template);
            
            if (success) {
                return ApiResponse.success("配置更新成功");
            } else {
                return ApiResponse.serverError("配置更新失败");
            }
        } catch (Exception e) {
            return ApiResponse.serverError("配置更新失败：" + e.getMessage());
        }
    }
}